 //类型模板控制层 
app.controller('typeTemplateController' ,function($scope,$controller,typeTemplateService,brandService,specificationService){

	$controller('baseController',{$scope:$scope});//继承

    //读取列表数据绑定到表单中
	$scope.findAll=function(){
		typeTemplateService.findAll().success(
			function(response){
				$scope.list=response;
			}
		);
	}

	//分页
	$scope.findPage=function(page,rows){
		typeTemplateService.findPage(page,rows).success(
			function(response){
				$scope.list=response.rows;
				$scope.paginationConf.totalItems=response.total;//更新总记录数
			}
		);
	}

	//查询实体
	$scope.findOne=function(id){
		typeTemplateService.findOne(id).success(
			function(response){
				$scope.entity= response;
				//json格式转化
				$scope.entity.brandIds = JSON.parse($scope.entity.brandIds);
				$scope.entity.specIds=JSON.parse($scope.entity.specIds);
				$scope.entity.customAttributeItems=JSON.parse($scope.entity.customAttributeItems);
			}
		);
	}

	//保存
	$scope.save=function(){
		var serviceObject;//服务层对象
		if($scope.entity.id!=null){//如果有ID
			serviceObject=typeTemplateService.update( $scope.entity ); //修改
		}else{
			serviceObject=typeTemplateService.add( $scope.entity  );//增加
		}
		serviceObject.success(
			function(response){
				if(response.flag){
                    alert(response.message);
					//重新查询
		        	$scope.reload();//重新加载
				}else{
					alert(response.message);
				}
			}
		);
	}


	//批量删除
	$scope.dele=function(){
		//获取选中的复选框
		typeTemplateService.dele( $scope.selectIds ).success(
			function(response){
				if(response.flag){
					alert(response.message)
					$scope.reload();//刷新列表
					$scope.selectIds=[];
				}
			}
		);
	}

	$scope.searchEntity={};//定义搜索对象
	$scope.list=[]
	//搜索
	$scope.search=function(page,rows){
		typeTemplateService.search(page,rows,$scope.searchEntity).success(
			function(response){
				$scope.list=response.rows;
				$scope.paginationConf.totalItems=response.total;//更新总记录数
			}
		);
	}


	//定义品牌列表数据
	$scope.brandList={data:[]};

	//读取品牌列表
        $scope.findBrandList=function () {
            brandService.selectOptionList().success(
                function (response) {
                    $scope.brandList={data:response};
            })

        }

        //定义规格列表数据
 		$scope.specList={data:[]};

	$scope.findSpecList=function () {
		specificationService.selectOptionList().success(
			function (response) {
			$scope.specList={data:response};
		})
	}


	$scope.initSelect=function () {
		$scope.findSpecList();
		$scope.findBrandList();
	}

	$scope.addTableRow=function () {
		$scope.entity.customAttributeItems.push({});
	}

	$scope.deleteTableRow=function (index) {
		$scope.entity.customAttributeItems.splice(index,1);
	}


	$scope.jsonToString=function (jsonString,key) {
		var  json=JSON.parse(jsonString);
		var  value="";
		for (var i=0; i<json.length;i++){
			if (i>0){
				value+=","
			}
				value+=json[i][key];
		}
			return value;
	}


});